import processing.core.PApplet;
import processing.core.PConstants;


public class Graph {
	private int x1, x2, y1, y2; 	//Coordinates of graph on screen 
									//(top-left, bottom-right corners)
	
	private int plotX1, plotX2, plotY1, plotY2;
	
	private int x_center, y_center;
	private int margin = 50;
	private int yStart = -1;
	
	String title = "GRAPH TITLE";
	String xLabel = "X-AXIS";
	String yLabel = "Y-AXIS";
	
	public Graph(int _x1, int _y1, int _x2, int _y2) {
		x1 = _x1;
		y1 = _y1;
		x2 = _x2;
		y2 = _y2;
		
		x_center = (x1+x2)/2;
		y_center = (y1+y2)/2;
		
		plotX1 = x1 + margin + 5;
		plotX2 = (x2 - margin);
		plotY1 = y2 - margin;
		plotY2 = y1 + margin; //(y2 - y1) - plotY1 +5;
	}
	
	public void setTitle(String t) {
		title = t;
	}
	
	public void setXLabel(String t) {
		xLabel = t;
	}
	
	public void setYLabel(String t) {
		yLabel = t;
	}
	
	private void drawAllLabels() {	
		GLOBAL.processing.textAlign(PConstants.CENTER);
		//Title
		GLOBAL.processing.text(title, x_center, y1+(margin/2));
		//X-Axis Label
		GLOBAL.processing.text(xLabel, x_center, y2-(margin/2)+12);
		//Y-Axis Label
		GLOBAL.processing.pushMatrix();
		GLOBAL.processing.translate((margin/2)-12, y_center);
		GLOBAL.processing.rotate(-PConstants.HALF_PI);
		GLOBAL.processing.text(yLabel, 0, 0);
		GLOBAL.processing.popMatrix();
	}
	
	private void drawAxis() {
		GLOBAL.processing.stroke(GLOBAL.colorGraphAxis);
		//Y-Axis
		GLOBAL.processing.line(x1+margin, y1+margin, x1+margin, y2-margin);
		//X-Axis
		GLOBAL.processing.line(x1+margin, y2-margin, x2-margin, y2-margin);
		
	}
	
	//Draw X-Axis Labels (Distance)
	public void drawXLabelsDistance(graphData data, int i1, int i2) {
		//increment values = i1 (minor tick) & i2 (major tick)
		
		for(float v = 0; v <= (int) data.getXMax(); v += i1) {
			if (v % i1 == 0) {     // If a tick mark
				float x = PApplet.map(v, data.getXMin(), data.getXMax(), plotX1+5, plotX2);   
				if (v % i2 == 0) {        // If a major tick mark
					GLOBAL.processing.text(PApplet.floor(v), x, plotY1 + 20);
					GLOBAL.processing.line(x, plotY1, x, plotY1-4);     // Draw major tick
				} else {
					GLOBAL.processing.line(x, plotY1, x, plotY1-2);     // Draw minor tick
				}
			}
		 }//End for loop
	}
	
	//Draw X-Axis Labels (X-data that always increments by 1)
	public void drawXLabelsSequential(graphData data) {
		for(float v = (int) data.getXMin(); v <= (int) data.getXMax(); v += 1) {
			float x = PApplet.map(v, data.getXMin(), data.getXMax(), plotX1+5, plotX2);   
			GLOBAL.processing.text(PApplet.floor(v), x, plotY1 + 20);
		 }//End for loop
	}
	
	public void drawData(graphData data, int minor, int major) {
		//Draw Background
		GLOBAL.processing.fill(GLOBAL.colorGraphBackground);
		GLOBAL.processing.rect(0, 100, 1024, 580);
		
		GLOBAL.processing.fill(0);	
		drawAllLabels();	//Draw title, x-labels, y-labels
		drawAxis();
		
		GLOBAL.processing.stroke(GLOBAL.colorGraphLine);
		GLOBAL.processing.strokeWeight(1);
		GLOBAL.processing.noFill();
		
		//Draw Line Graph
		GLOBAL.processing.beginShape();
		for(int i=0; i<data.rowCount; ++i) {
			float x = PApplet.map(data.getXCoord(i), data.getXMin(), data.getXMax(), plotX1, plotX2);
			float y = PApplet.map(data.getYCoord(i), data.getYMin(), data.getYMax(), plotY1, plotY2);
			
			GLOBAL.processing.vertex(x, y);
		}
		GLOBAL.processing.endShape();
		
		
		//Draw Axis Tick-Marks and Labels for Y-Axis
		GLOBAL.processing.stroke(GLOBAL.colorGraphAxis);
		
		if(yStart == -1)
			yStart = (int) data.getYMin();
		
		//Increments for tick-marks
		float i1 = minor;  //minor tick mark
		float i2 = major;  //major (labeled) tick mark
		  
		for(float v = yStart; v <= (int)data.getYMax(); v += i1) {
			if (v % i1 == 0) {     // If a tick mark
				float y = PApplet.map(v, data.getYMin(), data.getYMax(), plotY1, plotY2);   
				if (v % i2 == 0) {        // If a major tick mark
					float textOffset = GLOBAL.processing.textAscent()/2;  // Center vertically
					if (v == data.getYMin()) {
						textOffset = 0;                   // Align by the bottom
					} else if (v == data.getYMax()) {
						textOffset = GLOBAL.processing.textAscent();        // Align by the top
					}
					GLOBAL.processing.text(PApplet.floor(v), plotX1 - 20, y + textOffset);
					GLOBAL.processing.line(x1+margin, y, plotX1, y);     	// Draw major tick
				} else {
					GLOBAL.processing.line(x1+margin, y, plotX1, y);     // Draw minor tick
				}
			}
		 }//End for loop
	}
	
	public void setYStart(int y) {
		yStart = y;
	}
}
